package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.Customer;
import com.efast.dmom.mes.bean.CustomerPart;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.CustomerDataHandler;
import com.efast.dmom.mes.datahandlers.entity.CustomerPartDataHandler;
import com.efast.dmom.mes.service.CustomerService;
import com.efast.dmom.mes.util.StringVerifyUtil;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerDataHandler customerHandler;
	
	@Autowired
	private CustomerPartDataHandler customerPartDataHandler;

//	@Override
	public ResultBean save(Customer customer)  {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (!StringUtil.isNotBlank(customer.getInnerData().getName())
		|| !StringUtil.isNotBlank(customer.getInnerData().getCustomerCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查客户名称、客户编码是否填写!");
			return resultBean;
		}
	  Customer customerByCode = customerHandler.getCustomerByCode(customer.getInnerData().getCustomerCode());
	  if (customerByCode.getInnerData() != null) {
	  resultBean.setSuccess(false);
	  resultBean.setMessage("客户编码不唯一!");
	  return resultBean;
	  }
	  if (!StringVerifyUtil.isstring(customer.getInnerData().getCustomerCode())) {
		  resultBean.setSuccess(false);
		  resultBean.setMessage("客户编号不能由英文、字母、下划线组成!");
		  return resultBean;
	  }
		customerHandler.save(customer);
		resultBean.setSuccess(true);
		resultBean.setMessage("客户信息保存成功!");
		return resultBean;
	}

//	@Override
	public ResultBean delete(Customer customer) {
		ResultBean resultBean = new ResultBean(true, ' ');
		List<CustomerPart> customerPartList = customerPartDataHandler.getCustomerPartByCustomerId(customer.getInnerData().getId());
		if (customerPartList != null && customerPartList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该客户已被引用，无法删除!");
			return resultBean;
		}
		customerHandler.delete(customer);
		resultBean.setSuccess(true);
		resultBean.setMessage("客户信息删除成功!");
		return resultBean;
	}

//	@Override
	public ResultBean update(Customer customer)  {
		ResultBean resultBean = new ResultBean(true, ' ');
		Customer customerData = getById(customer.getInnerData().getId());

		if (customerData.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的客户不存在!");
			return resultBean;
		}
		if (!customer.getInnerData().getCustomerCode().equals(customerData.getInnerData().getCustomerCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("客户编码不可修改!");
			return resultBean;
		}
		if (!customerData.getInnerData().getName().equals(customer.getInnerData().getName())) {
			//Determine if the modified customer name is repeated
			Customer customerByName = customerHandler
					.getByName(customer.getInnerData().getName());
			if (customerByName.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改的客户名称不唯一!");
				return resultBean;
			}
		}
		customerHandler.update(customer);
		resultBean.setSuccess(true);
		resultBean.setMessage("客户信息修改成功!");
		return resultBean;
	}

	@Override
	public Customer getById(long id) {
		return customerHandler.getById(id);
	}

	@Override
	public Customer getByName(String name) {
		return customerHandler.getByName(name);
	}

	@Override
	public Customer getCustomerByCode(String customerCode) {
		return customerHandler.getCustomerByCode(customerCode);
	}

}
